﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;


namespace Business.Crypto
{
    public class Asymmetrical
    {

        private const string KEY_CONTAINER_NAME = "my key!";

        public static byte[] Encrypt(string text)
        {
            RSACryptoServiceProvider RSAEncrypt = CreateRSAMyAlgoritm;

            byte[] textToEncrypt = Encoding.Unicode.GetBytes(text);
            byte[] encryptedText = RSAEncrypt.Encrypt(textToEncrypt, true);

            return encryptedText;
        }
        public static string Decrypt(byte[] bytes)
        {
            RSACryptoServiceProvider RSADecrypt = CreateRSAMyAlgoritm;
            byte[] decrypted = RSADecrypt.Decrypt(bytes, true);

            return Encoding.Unicode.GetString(decrypted);
        }

        private static RSACryptoServiceProvider CreateRSAMyAlgoritm
        {
            get
            {
                CspParameters cspParams = new CspParameters();
                cspParams.KeyContainerName = KEY_CONTAINER_NAME;

                RSACryptoServiceProvider myAlg = new RSACryptoServiceProvider(cspParams);
                myAlg.PersistKeyInCsp = true;

                return myAlg;
            }
        }

    }
}
